package top.wilbert.collegelink.forum.controller;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;
import top.wilbert.collegelink.common.exception.BizCodeEnum;
import top.wilbert.collegelink.common.utils.Query;
import top.wilbert.collegelink.forum.entity.TalkEntity;
import top.wilbert.collegelink.forum.feign.ThirdPartFeignService;
import top.wilbert.collegelink.forum.service.TalkService;
import top.wilbert.collegelink.common.utils.PageUtils;
import top.wilbert.collegelink.common.utils.R;
import top.wilbert.collegelink.forum.vo.TalkCreateVo;
import top.wilbert.collegelink.forum.vo.TalkResponseVo;


/**
 * @author Augus
 * @email 2531496343@qq.com
 * @date 2023-02-28 14:34:36
 */
@RestController
@RequestMapping("forum/talk")
public class TalkController {
    @Autowired
    private TalkService talkService;

    @Autowired
    private ThirdPartFeignService thirdPartFeignService;

    /**
     * 转发话题
     *
     * @param talkId 转发的话题id
     * @param userId 当前用户id
     * @return
     */
//    @RequestMapping("/retransmission")
//    public R retransmission(@RequestParam Long talkId, @RequestParam Long userId) {
//        return R.ok();
//    }

    /**
     * 用户在论坛发布话题（带图片）
     * @param title
     * @param content
     * @param tag
     * @param creatorId
     * @param schoolId
     * @param images
     * @return
     */
    @RequestMapping("/publish/withImages")
    public R publish(@RequestParam String title, @RequestParam String content, @RequestParam String tag, @RequestParam Long creatorId, @RequestParam Integer schoolId, @RequestPart("images") MultipartFile[] images) {
        TalkEntity talkEntity = new TalkEntity();
        talkEntity.setTitle(title);
        talkEntity.setContent(content);
        talkEntity.setTag(tag);
        talkEntity.setCreatorId(creatorId);
        talkEntity.setSchoolId(schoolId);
        //上传图片，返回图片地址
        String imagesUrl = thirdPartFeignService.uploadImages(images);
        talkEntity.setImagesUrl(imagesUrl);
        //获取当前时间并保存
        Date date = new Date();
        talkEntity.setCreateTime(date);
        talkService.save(talkEntity);
        talkService.open(talkEntity.getTalkId());

        return R.ok();
    }

    /**
     * 用户在论坛发布话题（不带图片）
     *
     * @param talkCreateVo
     * @return
     */
    @RequestMapping("/publish/noImages")
    public R publish(@RequestBody TalkCreateVo talkCreateVo) {
        TalkEntity talkEntity = new TalkEntity();
        talkEntity.setTitle(talkCreateVo.getTitle());
        talkEntity.setContent(talkCreateVo.getContent());
        talkEntity.setTag(talkCreateVo.getTag());
        talkEntity.setCreatorId(talkCreateVo.getCreatorId());
        talkEntity.setSchoolId(talkCreateVo.getSchoolId());
        //获取当前时间并保存
        Date date = new Date();
        talkEntity.setCreateTime(date);
        talkService.save(talkEntity);
        talkService.open(talkEntity.getTalkId());

        return R.ok();
    }

    /**
     * 用户删除话题
     *
     * @param talkId
     * @return
     */
    @RequestMapping("/deleteById")
    public R deleteById(@RequestParam Long talkId, @RequestParam Long userId) {
        TalkEntity talkEntity = talkService.getById(talkId);
        if (userId != talkEntity.getCreatorId()) {
            return R.error(BizCodeEnum.ILLEGAL_OPERATION_EXCEPTION.getCode(), BizCodeEnum.ILLEGAL_OPERATION_EXCEPTION.getMessage());
        }
        talkService.close(talkId);
        talkService.removeById(talkId);

        return R.ok();
    }

    /**
     * 用户修改话题内容
     *
     * @param talkId
     * @param content
     * @param tag
     * @param images
     * @return
     */
    @RequestMapping("/modified")
    public R openModified(@RequestParam Long talkId, @RequestParam String content, @RequestParam String tag, @RequestPart("images") MultipartFile[] images) {
        talkService.close(talkId);
        TalkEntity talkEntity = new TalkEntity();
        talkEntity.setTalkId(talkId);
        talkEntity.setContent(content);
        talkEntity.setTag(tag);
        //上传图片，返回图片地址
        String imageUrl = thirdPartFeignService.uploadImages(images);
        talkEntity.setImagesUrl(imageUrl);
        //获取当前时间并保存
        Date date = new Date();
        talkEntity.setModifiedTime(date);
        talkService.updateById(talkEntity);
        talkService.open(talkId);

        return R.ok();
    }

    /**
     * 用户修改话题状态
     *
     * @param talkId
     * @param status
     * @return
     */
//    @RequestMapping("/updateStatus")
//    public R updateStatus(@RequestParam Long talkId, @RequestParam Integer status) {
//        if (status == ForumConstant.StatusEnum.TALK_OPEN.getCode()) {
//            talkService.open(talkId);
//        } else {
//            talkService.close(talkId, status);
//        }
//
//        return R.ok();
//    }

    /**
     * 用户获取话题草稿箱列表
     * @param params
     * @param userId
     * @return
     */
//    @RequestMapping("/selectMyDraftTalkList")
//    public R getDraftList(@RequestParam Map<String, Object> params, @RequestParam Long userId){
//        PageUtils page = talkService.selectMyDraftTalkList(new Query<TalkResponseVo>().getPage(params), userId);
//
//        return R.ok().setData(page);
//    }

    /**
     * 用户获取自己发布过的话题列表
     * @param params
     * @param userId
     * @return
     */
    @RequestMapping("/selectMyTalkList")
    public R getTalkList(@RequestParam Map<String, Object> params, @RequestParam Long userId){
        PageUtils page = talkService.selectMyTalkList(new Query<TalkResponseVo>().getPage(params), userId);

        return R.ok().setData(page);
    }

    /**
     * 根据学校id获取话题列表
     * @param params
     * @param schoolId
     * @param userId
     * @return
     */
    @RequestMapping("/listInfoBySchoolId")
    public R listInfoBySchoolId(@RequestParam Map<String, Object> params, @RequestParam Integer schoolId, @RequestParam Long userId) {
        PageUtils page = talkService.selectSchoolTalkList(new Query<TalkResponseVo>().getPage(params), schoolId, userId);

        return R.ok().setData(page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("forum:talk:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = talkService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("forum:talk:info")
    public R info(@PathVariable("id") Long id) {
        TalkEntity talk = talkService.getById(id);

        return R.ok().put("talk", talk);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("forum:talk:save")
    public R save(@RequestBody TalkEntity talk) {
        talkService.save(talk);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("forum:talk:update")
    public R update(@RequestBody TalkEntity talk) {
        talkService.updateById(talk);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("forum:talk:delete")
    public R delete(@RequestBody Long[] ids) {
        talkService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
